#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/7/16  12:40
# @Author: 余浪人
# @email: yulangren520@gmail.com
from flask_login import login_required
import platform, psutil, datetime
from apps.control import sys_bp
from flask import render_template, request, jsonify, current_app, url_for
from apps.forms.system_form import TL_System_Form, TL_Ad_Form, TL_Blogroll_Form, TL_Redis_Form, TL_Notice_Form, TL_Pay_Form, TL_Email_Form, TL_Save_Form
from apps.lib.cache_sys import sys_cache,cache
from apps.lib.public import edit_data, add_data, check_obj, real_delete_data, real_deletes, page_auto, redis_
from apps.lib.token import Verify_authorization
from apps.models.content_model import TL_Content_Model, TL_Column_model
from apps.models.system_model import db, TL_System_Model, TL_Ad_Model, TL_Blogroll_Model, TL_Redis_Model, TL_Notice_Model, \
    TL_PayConf_Model, TL_Email_Model, TL_Save_Model
@sys_bp .route ('/manage',endpoint ='base_frame',methods =['GET'])
@login_required
def base_frame ():
    return render_template ('system_template/base_frame.html')
@sys_bp .route ('/top_frame',endpoint ='top_frame',methods =['GET'])
@login_required
def top_frame ():
    return render_template ('system_template/top_frame.html')
@sys_bp .route ('/left_frame',endpoint ='left_frame',methods =['GET'])
@login_required
def left_frame ():
    return render_template ('system_template/left_frame.html')
@sys_bp .errorhandler (404 )
def not_found_error (OO0O0OO00O0O0O000 ):
    return render_template ('error_template/error_400.html'),200
@sys_bp .route ('/main_frame',endpoint ='main_frame',methods =['GET'])
@login_required
def main_frame ():
    OOO00O00OO00OOOO0 =TL_Content_Model .query .filter ().count ()
    OO0O0000OOOO00000 =TL_Column_model .query .filter ().count ()
    O0OOOO0OO0OOOO0OO =TL_Ad_Model .query .filter ().count ()
    O000OO00OO000000O =TL_Blogroll_Model .query .filter ().count ()
    O000O0O00O0O0OOO0 =TL_System_Model .query .first ().create_time .strftime ('%Y-%m-%d').split ('-')
    OOOOO00O000O000O0 =datetime .datetime .now ().strftime ('%Y-%m-%d').split ('-')
    OO0OO0O0OOOOOO00O =datetime .datetime (int (O000O0O00O0O0OOO0 [0 ]),int (O000O0O00O0O0OOO0 [1 ]),int (O000O0O00O0O0OOO0 [2 ]))
    O00O0OO00OO0OOO0O =datetime .datetime (int (OOOOO00O000O000O0 [0 ]),int (OOOOO00O000O000O0 [1 ]),int (OOOOO00O000O000O0 [2 ]))
    O0O00000OO000O00O =(O00O0OO00OO0OOO0O -OO0OO0O0OOOOOO00O ).days
    OO000O00OO0OO00O0 =psutil .virtual_memory ()
    O0O000OO00OOO0000 =str (round (OO000O00OO0OO00O0 .total /1024 /1024 ))
    O0O000O0O0OOO00OO =str (round (OO000O00OO0OO00O0 .used /1024 /1024 ))
    O00O0OO0OOOOOO0O0 =str (round (OO000O00OO0OO00O0 .percent ))
    OO0OO00O0OO0000OO =str (round (OO000O00OO0OO00O0 .free /1024 /1024 ))
    O0OOO000OO000OOO0 =platform .system ()
    OO0O0O0OOOO0OO00O ='0.1'
    O0O00O0O0OO0O000O =TL_Content_Model .query .order_by (TL_Content_Model .id .desc ()).filter (TL_Content_Model .status !=-1 ).limit (6 ).all ()
    O0O0O000O0O0OOO00 ={"count":{"doc":OOO00O00OO00OOOO0 ,"col":OO0O0000OOOO00000 ,"ad":O0OOOO0OO0OOOO0OO ,"link":O000OO00OO000000O },"version":OO0O0O0OOOO0OO00O ,"sys_os":O0OOO000OO000OOO0 ,'mem':{"total":O0O000OO00OOO0000 ,"used":O0O000O0O0OOO00OO ,"use_per":O00O0OO0OOOOOO0O0 ,"free":OO0OO00O0OO0000OO },"sys_has_run":O0O00000OO000O00O ,"doc_list":O0O00O0O0OO0O000O }
    return render_template ('system_template/main_frame.html',data =O0O0O000O0O0OOO00 )
@sys_bp .route ('/favicon.ico',endpoint ='favicon',methods =['GET'])
def favicon ():
    O000OO00OOO0O0OO0 =sys_cache ()
    OOOO000O0O0OOO000 =O000OO00OOO0O0OO0 .web_ico [1 :]
    if len (OOOO000O0O0OOO000 )<10 :OOOO000O0O0OOO000 ='images/favicon.ico'
    return current_app .send_static_file (OOOO000O0O0OOO000 )
@sys_bp .route ('/system',endpoint ='system',methods =['GET','POST'])
@login_required
def system ():
    O000O000OOOOO000O =TL_System_Model .query .first ()
    if request .method =='POST':
        cache .delete ('sys_cache')
        return edit_data (TL_System_Form ,TL_System_Model ,db ,O000O000OOOOO000O .id )
    O00000OOOO00O00OO =TL_System_Form (data =dict (O000O000OOOOO000O ))
    return render_template ('system_template/system.html',form =O00000OOOO00O00OO )
@sys_bp .route ('/notice_list',endpoint ='notice_list',methods =['GET','DELETE','DELETES'])
@login_required
def notice_list ():
    if request .method =='DELETE':
        return real_delete_data (TL_Notice_Model ,db ,request .form .get ('uid'))
    elif request .method =='DELETES':
        return real_deletes (TL_Notice_Model ,db ,request .form .get ('id_array').split ('#TL#'))
    OO00O0O0O0OO0000O ,OO0O00O00O00000OO =page_auto (TL_Notice_Model .query .order_by (TL_Notice_Model .id .desc ()))
    return render_template ('system_template/notice_list.html',data_list =OO00O0O0O0OO0000O ,page =OO0O00O00O00000OO )
@sys_bp .route ('/notice',endpoint ='notice',methods =['GET','POST'])
@login_required
@Verify_authorization
def notice ():
    if request .method =='POST':
        return add_data (TL_Notice_Model ,TL_Notice_Form ,db )
    return render_template ('system_template/notice.html',form =TL_Notice_Form ())
@sys_bp .route ('/notice_edit_<int:id>',endpoint ='notice_edit',methods =['GET','POST'])
@login_required
def notice_edit (id ):
    OOO000O0OO0000O00 =TL_Notice_Model .query .filter_by (id =id ).first ()
    if request .method =='POST'and not check_obj (OOO000O0OO0000O00 ):
        return edit_data (TL_Notice_Form ,TL_Notice_Model ,db ,OOO000O0OO0000O00 .id )
    if not check_obj (OOO000O0OO0000O00 ):
        return render_template ('system_template/notice.html',form =TL_Notice_Form (data =dict (OOO000O0OO0000O00 )))
@sys_bp .route ('/advertising_list',endpoint ='advertising_list',methods =['GET','DELETE','DELETES'])
@login_required
def advertising_list ():
    if request .method =='DELETE':
        return real_delete_data (TL_Ad_Model ,db ,request .form .get ('uid'))
    elif request .method =='DELETES':
        return real_deletes (TL_Ad_Model ,db ,request .form .get ('id_array').split ('#TL#'))
    O00OO00OO00OO0O00 ,OO0OOOOO0OO0OOOOO =page_auto (TL_Ad_Model .query .order_by (TL_Ad_Model .id .desc ()))
    return render_template ('system_template/advertising_list.html',data_list =O00OO00OO00OO0O00 ,page =OO0OOOOO0OO0OOOOO )
@sys_bp .route ('/advertising',endpoint ='advertising',methods =['GET','POST'])
@login_required
@Verify_authorization
def advertising ():
    if request .method =='POST':
        return add_data (TL_Ad_Model ,TL_Ad_Form ,db )
    return render_template ('system_template/advertising.html',form =TL_Ad_Form ())
@sys_bp .route ('/advertising_<int:id>',endpoint ='advertising_edit',methods =['GET','POST'])
@login_required
def advertising_edit (id ):
    O0O00OO000OOO0OO0 =TL_Ad_Model .query .filter_by (id =id ).first ()
    if request .method =='POST'and not check_obj (O0O00OO000OOO0OO0 ):
        return edit_data (TL_Ad_Form ,TL_Ad_Model ,db ,id )
    if not check_obj (O0O00OO000OOO0OO0 ):
        return render_template ('system_template/advertising.html',form =TL_Ad_Form (data =dict (O0O00OO000OOO0OO0 )))
@sys_bp .route ('/blogroll_list',endpoint ='blogroll_list',methods =['GET','DELETE','DELETES'])
@login_required
def blogroll_list ():
    if request .method =='DELETE':
        return real_delete_data (TL_Blogroll_Model ,db ,request .form .get ('uid'))
    elif request .method =='DELETES':
        return real_deletes (TL_Blogroll_Model ,db ,request .form .get ('id_array').split ('#TL#'))
    OOOO0O0OO000O000O ,OOO00O0O000O0O00O =page_auto (TL_Blogroll_Model .query .order_by (TL_Blogroll_Model .id .desc ()))
    return render_template ('system_template/blogroll_list.html',data_list =OOOO0O0OO000O000O ,page =OOO00O0O000O0O00O )
@sys_bp .route ('/blogroll',endpoint ='blogroll',methods =['GET','POST'])
@login_required
@Verify_authorization
def blogroll ():
    if request .method =='POST':
        return add_data (TL_Blogroll_Model ,TL_Blogroll_Form ,db )
    return render_template ('system_template/blogroll.html',form =TL_Blogroll_Form ())
@sys_bp .route ('/blogroll_<int:id>',endpoint ='blogroll_edit',methods =['GET','POST'])
@login_required
def blogroll_edit (id ):
    OO000O0000O00O0OO =TL_Blogroll_Model .query .filter_by (id =id ).first ()
    if request .method =='POST'and not check_obj (OO000O0000O00O0OO ):
        return edit_data (TL_Blogroll_Form ,TL_Blogroll_Model ,db ,id )
    if not check_obj (OO000O0000O00O0OO ):
        return render_template ('system_template/blogroll.html',form =TL_Blogroll_Form (data =dict (OO000O0000O00O0OO )))
@sys_bp .route ('/pay_conf',endpoint ='pay_conf',methods =['GET','POST'])
@login_required
def pay_conf ():
    OOOO00OOOOOOO00O0 =TL_PayConf_Model .query .filter_by ().first ()
    if request .method =='POST'and not check_obj (OOOO00OOOOOOO00O0 ):
        return edit_data (TL_Pay_Form ,TL_PayConf_Model ,db ,OOOO00OOOOOOO00O0 .id )
    if not check_obj (OOOO00OOOOOOO00O0 ):
        return render_template ('system_template/pay_conf.html',form =TL_Pay_Form (data =dict (OOOO00OOOOOOO00O0 )))
@sys_bp .route ('/redis_conf',endpoint ='redis_conf',methods =['GET','POST'])
@login_required
def redis_conf ():
    O00OOOO0O0O0OOOOO =TL_Redis_Model .query .first ()
    if request .method =='POST':
        try :
            return edit_data (TL_Redis_Form ,TL_Redis_Model ,db ,O00OOOO0O0O0OOOOO .id )
        except :
            return jsonify ({"status":-1 ,"msg":"保存成功,可能连接异常!"})
    OO0OO00OO0O0OOOOO =TL_Redis_Form (data =dict (O00OOOO0O0O0OOOOO ))
    return render_template ('system_template/redis_conf.html',form =OO0OO00OO0O0OOOOO )
@sys_bp .route ('/email_conf',endpoint ='email_conf',methods =['GET','POST'])
@login_required
def email_conf ():
    OO0000000OO000000 =TL_Email_Model .query .first ()
    if request .method =='POST':
        return edit_data (TL_Email_Form ,TL_Email_Model ,db ,OO0000000OO000000 .id )
    OO00O00O0000O00O0 =TL_Email_Form (data =dict (OO0000000OO000000 ))
    return render_template ('system_template/email_conf.html',form =OO00O00O0000O00O0 )
@sys_bp .route ('/save_conf',endpoint ='save_conf',methods =['GET','POST'])
@login_required
def save_conf ():
    OOO000O00O00000O0 =TL_Save_Model .query .first ()
    if request .method =='POST':
        return edit_data (TL_Save_Form ,TL_Save_Model ,db ,OOO000O00O00000O0 .id )
    O0O0O0OOOOOO0O000 =TL_Save_Form (data =dict (OOO000O00O00000O0 ))
    return render_template ('system_template/save_conf.html',form =O0O0O0OOOOOO0O000 )
